package com.jinan.algorithm.slidingWindow;

/**
 * 子数组最大平均数
 * 给一个整数数组,找出平均数最大且长度为k的下标连续的子数组，并输出该最大平均数。
 */
public class SubArrMaxAverage {
    public static void main(String[] args) {
        int[] nums = {1, 12, -5, -6, 50, 3};
        System.out.println(findMaxAverage(nums, 4));
    }

    public static double findMaxAverage(int[] nums, int k) {
        double windowSum = 0;
        double maxSum = 0;
        for (int i = 0; i < k; i++) {
            windowSum += nums[i];
        }
        for (int i = k; i < nums.length; i++) {
            windowSum = windowSum - nums[i - k] + nums[i];
            maxSum = Math.max(windowSum, maxSum);
        }
        return maxSum / k;
    }

}
